/* 
 * This code is part of the smartlink project, see
 * 
 * 		http://code.google.com/p/smartlink/.
 * 
 * Copyright (C) 2008 Oliver Noelle
 * 
 * SmartLink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SmartLink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SmartLink.  If not, see <http://www.gnu.org/licenses/>.
 **/
package com.google.bookmarkswrapper;

import org.smartlink.Server;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

/** Abstract class to return a result text from a Bookmark to smartlink */
public abstract class AbstractResultActivity extends Activity {

	private static final String TAG = "AbstractResultActivity";

	public AbstractResultActivity() {
		super();
	}

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		// setContentView(R.layout.translucent_background);
	}

	@Override
	protected void onStart() {
		super.onStart();
		Intent intent = getIntent();
		String action = intent.getAction();
		if (action.equals(Server.USE_TARGET_ACTION)) {
			setResult(RESULT_OK, getResult(intent.getData()));
			finish();
		} else {
			Log.w(TAG, "started with unexpected action: " + action);
			return;
		}
	}

	protected abstract String getResult(Uri data);

	/** gets the specified column of the Bookmark database entry. */
	protected String getColumn(Uri uri, String column) {
		String[] projection = new String[] { column };
		Cursor cur = managedQuery(uri, projection, // Which columns to return.
				null, // WHERE clause
				null); // Order-by clause.
		if (!cur.first()) {
			return null;
		}
		return cur.getString(cur.getColumnIndex(column));
	}
}